// two-lists.cpp
#include <stdio.h>
#include <yawl.h>

// UC issue: no default arg in templ.
template <class T>
void move_to_list(TListBox *lb, T start, T finish)
{
    for(;start != finish; ++start) {
      lb->add(*start);
    }
}

char *names[] = {"mary","jane","john","freddy","alice",NULL};


class MyWin: public TFrameWindow {
public:
  TListBox *fb, *tb;

  MyWin();
  void move_between_lists(TListBox *fb, TListBox *tb, int inc1, int inc2);
    void on_move_back(int)      {  move_between_lists(tb,fb,1,0);  }
  void on_move_forward(int)  {  move_between_lists(fb,tb,1,0); }
  void on_up(int)                   {  move_between_lists(tb,tb,-1,-1);  }
  void on_down(int)               {	move_between_lists(tb,tb,+1,+1);  }	  

  
};

MyWin::MyWin()
{

    set_text("A Simple Layout");

	Layout(this)
		<< "From"  << "To"  << Next
		<< Listbox(150) << NamedAs(&fb)
		<< Listbox(150) << NamedAs(&tb)  	<< Next
		<< Button("<<",(EH)on_move_back)
		<< Button(">>",(EH)on_move_forward) << Next
		<< Button("Up",(EH)on_up)
		<< Button("Down",(EH)on_down)
      ;

	 move_to_list(fb,names,names+5); 
	 tb->add("bonzo");
	 fb->selected(0);
	 tb->selected(0);

}

void MyWin::move_between_lists(TListBox *fb, TListBox *tb, int inc1, int inc2)
{
	  char str[40];
	  if (fb->count() == 0) return; // nothing to move!
	  int idx = fb->selected();
	  if (idx == -1) idx = 0;
	  fb->get_text(idx,str);
	  fb->remove(idx);
	  fb->selected(idx+inc1);
	  tb->insert(tb->selected(),str);
	  if (inc2 != 0) tb->selected(idx+inc2);
}

int main(int,char **) 
{
 MyWin w;
 w.show();
 w.run();
 return 0;
}


 